#include "robotwidget.h"
#include "ui_robotwidget.h"

RobotWidget::RobotWidget(quint16 id, QString host, quint16 port, Camera *camera, QColor *color, QWidget *parent) :
    QFrame(parent),
    m_ui(new Ui::RobotWidget)
{
    m_ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);

    robot = new Robot(id, host, port, camera, this);
    robot->setStateLabel(m_ui->stateLabel);
    robot->setEvalPositionLabel(m_ui->robotPositionLabel);
    robot->setTruePositionLabel(m_ui->robotTruePositionLabel);

    if (color != 0) {
        robot->getColor()->setNamedColor(color->name());
    }

    updateColorSample();

    m_ui->hostLabel->setText(robot->toString());
}

RobotWidget::~RobotWidget()
{
    delete m_ui;
}

Robot* RobotWidget::getRobot()
{
    return robot;
}

void RobotWidget::updateColorSample()
{
    m_ui->colorSample->setStyleSheet(tr("QPushButton { background-color: %1; }").arg(robot->getColor()->name()));
}

void RobotWidget::setColor(QColor color)
{
    robot->getColor()->setRed(color.red());
    robot->getColor()->setGreen(color.green());
    robot->getColor()->setBlue(color.blue());
}

void RobotWidget::on_reconnectButton_clicked()
{
    robot->connectToHost();
}

void RobotWidget::on_sendButton_clicked()
{
    robot->sendDestination(m_ui->destXSpinBox->value(),
                           m_ui->destYSpinBox->value(), m_ui->destZSpinBox->value());
}

void RobotWidget::on_closeButton_clicked()
{
    close();
}

void RobotWidget::setLogTextEdit(QTextEdit *logTextEdit)
{
    robot->setLogTextEdit(logTextEdit);
}

void RobotWidget::on_modeButton_clicked()
{
    if (m_ui->modeButton->text() == "Go")
    {
        robot->sendGo();
        m_ui->modeButton->setText("Stop");
    } else {
        robot->sendStop();
        m_ui->modeButton->setText("Go");
    }
}

void RobotWidget::on_colorSample_clicked()
{
    robot->randomColor();
    updateColorSample();
}
